home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 551-575 / disk_556 / scheme2c / build.rexx < prev    next >
OS/2 REXX Batch file  |  1992-05-06  |  5KB  |  168 lines

  1. /*
  2.  * A hack to make building other programs easier, by extracting the
  3.  * commands needed to build the file from it's source.
  4.  *
  5.  *    build [name NAME] [flags COMMAND=FLAGS ...] [file] FILE [FILE ...] 
  6.  *
  7.  * Name defaults to "build". Flags causes the following arguments to be parsed
  8.  * for extra flags to be added to each COMMAND. All input FILEs are scanned
  9.  * for build lines to be issued to the system. If flags is present, file is
  10.  * required.
  11.  *
  12.  * In scanning a file, build ignores all lines until it sees a line containing
  13.  * the string "===NAME", for whatever value name has, defaulting to build. All
  14.  * lines following are then processed until a line containing "===endNAME" is
  15.  * encountered. Delimiter and control are set by parsing the line ===NAME line
  16.  * as '===NAME delimiter DELIMITER control CONTROL'
  17.  *
  18.  * In processing the build lines, two tokens have special significance. They
  19.  * are called delimiter and control, and are normally '%' and ';'. The line is
  20.  * first split on the control character, into the front and back halves. If
  21.  * there is a delimiter in the front half, any text preceding it is discarded.
  22.  * If there is a delimiter in the back half, any text following it is discarded.
  23.  *
  24.  * The front half is treated as a command to be issued to the system. If the
  25.  * first word of the front half is matched by a the command part of a flags
  26.  * argument, then the flags part of that argument is inserted into the command
  27.  * after the first word. I.e. the command issued is "command FLAGS rest of line".
  28.  * The back half of the command is then checked, and the above comamnd is issued
  29.  * if it is called for.
  30.  *
  31.  * The back half is assumed to be of the form 'output= FILE input= FILE ...'.
  32.  * If any of the input files are newer than the output file, then the command
  33.  * in the front half will be executed.  If either input or output keywords are
  34.  * missing, the command is always issued.
  35.  *
  36.  * As a convenience, if the back half of a line is empty, then it will be
  37.  * executed if the previous command was executed.
  38.  */
  39.  
  40. /* Get the support code */
  41. if ~show('Libraries', 'rexxsupport.library') then do
  42.     if ~addlib('rexxsupport.library', 0, -30) then do
  43.         say "Can't open rexxsupport.library!"
  44.         exit
  45.         end
  46.     end
  47.  
  48. flags. = ''
  49. flags = 0
  50. name = 'build'
  51. files = ''
  52.  
  53. /* Parse them arguments */
  54. args = arg(1)
  55. do i = 1 to words(args)
  56.     select
  57.         when upper(word(args, i)) = 'NAME' then do
  58.             flags = 0
  59.             i = i + 1
  60.             name = word(args, i)
  61.             end
  62.         when upper(word(args, i)) = 'FILE' then do
  63.             flags = 0
  64.             i = i + 1
  65.             files = word(args, i)
  66.             end
  67.         when upper(word(args, i)) = 'FLAGS' then flags = 1
  68.         when flags then do
  69.             parse value word(args, i) with command '=' flag
  70.             if flag = '' then
  71.                 flags.currentcommand = flags.currentcommand word(args, i)
  72.             else do
  73.                 currentcommand = command
  74.                 flags.command = flag
  75.                 end
  76.             end
  77.         otherwise files = files word(args, i)
  78.         end
  79.     end
  80.  
  81. /*
  82.  * loop over the file names.
  83.  */
  84. files = expand(files)
  85. do i = 1 to words(files)
  86.     call buildfile(word(files, i), name)
  87.     end
  88.  
  89. exit 0
  90.  
  91. /*
  92.  * Scan a file, looking for the build instructions.
  93.  */
  94. buildfile: procedure expose flags.
  95.     parse arg file, name
  96.  
  97.     delim = '%'
  98.     control = ';'
  99.  
  100.     if ~open(input, file) then do
  101.         say "Can't open file" file
  102.         return 10
  103.         end
  104.  
  105.     /* Search for the section with command in it */
  106.     search = '==='upper(name)
  107.     do until index(upper(line), search) ~= 0
  108.         if eof(input) then do
  109.             say "No actions found for" name "in file" file
  110.             return 5
  111.             end
  112.         line = readln(input)
  113.         end
  114.  
  115.     /* Check for control/delimter settings */
  116.     parse var line 'control' new .
  117.     if new ~= '' then control = new
  118.     parse var line 'delimiter' new .
  119.     if new ~= '' then delimiter = new
  120.  
  121.     /* Process the command lines */
  122.     search = '===END'upper(name)
  123.     line = readln(input)
  124.     do while index(upper(line), search) = 0
  125.         parse var line command (control) files
  126.  
  127.         /* Check files section */
  128.         parse var files files (delim)
  129.         parse var files "output=" outfile "input=" infiles
  130.         if outfile = "" | infiles = "" then docommand = 1
  131.         else do
  132.             instamp = makestamp(strip(outfile))
  133.             docommand = 0
  134.             do i = 1 to words(infiles)
  135.                 if instamp < makestamp(word(infiles, i)) then do
  136.                     docommand = 1
  137.                     leave
  138.                     end
  139.                 end
  140.             end
  141.  
  142.         /* Build command to execute */ 
  143.         parse var command (delim) new flags
  144.         if new ~= "" then command = new
  145.         else parse var command command flags
  146.         if command ~= "" & docommand then do
  147.             say command flags
  148.             address command command flags.command flags
  149.             if rc ~= 0 then exit rc
  150.             end
  151.         line = readln(input)
  152.  
  153.         if eof(input) then do
  154.             say "No end to build section"
  155.             exit 10
  156.             end
  157.         end
  158.  
  159.     call close input
  160.     return 0
  161.  
  162. /* Get a files creation date as a numeric string */
  163. makestamp: procedure
  164.     arg filename
  165.  
  166.     parse value statef(filename) with . . . . d m t .
  167.     return right(d, 4, 0) || right(m, 4, 0) || right(t, 4, 0)
  168.